{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c6901d88",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "from torch.utils.data import DataLoader, TensorDataset\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d829550",
   "metadata": {},
   "source": [
    "GRU的使用  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([32, 5, 20])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 32, 20])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "input_size: int = 10\n",
    "hidden_size: int = 20\n",
    "num_layers: int = 1\n",
    "seq_num = 5\n",
    "batch_size = 32\n",
    "\n",
    "\n",
    "input = torch.randn(batch_size,seq_num,input_size)\n",
    "h0 = torch.randn(num_layers,batch_size,hidden_size)\n",
    "\n",
    "\n",
    "model = torch.nn.RNN(input_size,hidden_size,num_layers,batch_first=True)\n",
    "a,b = model(input,h0)\n",
    "display(a.shape,b.shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (a[:,-1,:] == b[0])\n",
    "torch.equal(a[:,-1,:] , b[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([32, 5, 20])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 32, 20])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "input_size: int = 10\n",
    "hidden_size: int = 20\n",
    "num_layers: int = 1\n",
    "seq_num = 5\n",
    "batch_size = 32\n",
    "\n",
    "\n",
    "input = torch.randn(batch_size,seq_num,input_size)\n",
    "h0 = torch.randn(num_layers,batch_size,hidden_size)\n",
    "\n",
    "\n",
    "model = torch.nn.GRU(input_size,hidden_size,num_layers,batch_first=True)\n",
    "a,b = model(input,h0)\n",
    "display(a.shape,b.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "243372ef",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b3f2b5c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-6.0360e-01],\n",
       "         [-2.1670e-01],\n",
       "         [-2.4048e+00],\n",
       "         [ 3.6592e-01],\n",
       "         [-3.8299e-01],\n",
       "         [ 1.2503e-01],\n",
       "         [-4.8407e-01],\n",
       "         [ 8.4418e-01],\n",
       "         [ 2.8317e-01],\n",
       "         [ 1.7407e+00]],\n",
       "\n",
       "        [[-3.2983e-01],\n",
       "         [-3.3263e-01],\n",
       "         [ 6.2075e-01],\n",
       "         [-8.7732e-01],\n",
       "         [ 4.9887e-01],\n",
       "         [ 1.0435e-01],\n",
       "         [ 3.7523e-01],\n",
       "         [-1.5191e+00],\n",
       "         [-2.1403e+00],\n",
       "         [-2.5487e+00]],\n",
       "\n",
       "        [[ 1.7762e-01],\n",
       "         [-3.4410e-01],\n",
       "         [-1.2106e+00],\n",
       "         [-8.8019e-01],\n",
       "         [-6.7788e-01],\n",
       "         [-9.8444e-01],\n",
       "         [ 6.8927e-01],\n",
       "         [-5.6685e-01],\n",
       "         [-4.3909e-01],\n",
       "         [-2.6745e+00]],\n",
       "\n",
       "        [[ 8.0532e-01],\n",
       "         [-2.3071e-01],\n",
       "         [-5.6639e-03],\n",
       "         [ 1.9151e+00],\n",
       "         [-1.3510e+00],\n",
       "         [ 4.7745e-01],\n",
       "         [-3.5541e-02],\n",
       "         [ 5.8824e-01],\n",
       "         [ 2.7581e-01],\n",
       "         [-1.1975e+00]],\n",
       "\n",
       "        [[ 9.4297e-01],\n",
       "         [-4.2819e-01],\n",
       "         [ 2.0612e-01],\n",
       "         [ 1.3772e+00],\n",
       "         [-6.8226e-01],\n",
       "         [-7.2647e-01],\n",
       "         [ 2.0127e-01],\n",
       "         [ 7.3110e-01],\n",
       "         [-3.5371e-01],\n",
       "         [ 1.1542e-01]],\n",
       "\n",
       "        [[ 1.1865e-01],\n",
       "         [ 1.2342e-01],\n",
       "         [ 1.4094e+00],\n",
       "         [ 3.7863e-01],\n",
       "         [ 3.7625e-01],\n",
       "         [-1.4789e-01],\n",
       "         [-7.0369e-01],\n",
       "         [-2.9119e+00],\n",
       "         [ 5.2441e-01],\n",
       "         [-2.0297e-01]],\n",
       "\n",
       "        [[ 1.5132e+00],\n",
       "         [-2.9119e-01],\n",
       "         [ 9.3398e-01],\n",
       "         [-5.1434e-01],\n",
       "         [-1.6888e+00],\n",
       "         [ 2.5536e-01],\n",
       "         [ 8.3084e-01],\n",
       "         [-5.2917e-01],\n",
       "         [ 3.5174e-01],\n",
       "         [-1.2057e+00]],\n",
       "\n",
       "        [[ 6.8275e-01],\n",
       "         [-8.3266e-01],\n",
       "         [ 7.5705e-01],\n",
       "         [ 6.1804e-02],\n",
       "         [ 8.6185e-01],\n",
       "         [ 8.2724e-01],\n",
       "         [-2.3783e-03],\n",
       "         [-6.7071e-01],\n",
       "         [ 1.7140e-01],\n",
       "         [ 5.3204e-01]],\n",
       "\n",
       "        [[-4.2742e-01],\n",
       "         [-5.0324e-01],\n",
       "         [ 5.8974e-01],\n",
       "         [ 3.2985e-01],\n",
       "         [-2.0422e-01],\n",
       "         [-5.4735e-02],\n",
       "         [ 6.4020e-01],\n",
       "         [-1.0082e+00],\n",
       "         [ 1.6026e+00],\n",
       "         [-4.8868e-01]],\n",
       "\n",
       "        [[ 1.4471e-01],\n",
       "         [-8.6848e-01],\n",
       "         [-2.7506e+00],\n",
       "         [-2.5264e-01],\n",
       "         [-5.1493e-01],\n",
       "         [-1.0468e+00],\n",
       "         [ 1.5256e+00],\n",
       "         [ 1.3008e+00],\n",
       "         [ 4.1563e-01],\n",
       "         [ 1.6006e+00]]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "tensor([[-0.0733],\n",
       "        [-0.6149],\n",
       "        [-0.6911],\n",
       "        [ 0.1241],\n",
       "        [ 0.1383],\n",
       "        [-0.1036],\n",
       "        [-0.0344],\n",
       "        [ 0.2388],\n",
       "        [ 0.0476],\n",
       "        [-0.0446]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "torch.Size([10, 10, 1])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x=torch.randn(10,10,1)\n",
    "y=torch.mean(x,axis=1)\n",
    "display(x,y)\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "30c28965",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = TensorDataset(x,y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "11f4f674",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([10, 1])\n",
      "tensor([1.0438])\n"
     ]
    }
   ],
   "source": [
    "for x,y in d:\n",
    "    print(x.shape)\n",
    "    print(y)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cb3b4a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73085cdd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 10, 1])\n",
      "torch.Size([3, 1])\n"
     ]
    }
   ],
   "source": [
    "dl = DataLoader(d,batch_size=3,shuffle=False)\n",
    "for _ in dl:\n",
    "    print(_[0].shape)\n",
    "    print(_[1].shape)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d599c882",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(1,2).dtype\n",
    "torch.FloatTensor(torch.randn(1,2)).dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1c545f0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-0.8636, -1.4920],\n",
       "         [ 0.0556, -0.0949]],\n",
       "\n",
       "        [[ 0.9540, -0.6032],\n",
       "         [ 0.8379,  0.0307]],\n",
       "\n",
       "        [[ 0.8680, -0.0508],\n",
       "         [ 1.0185,  0.9822]],\n",
       "\n",
       "        [[ 1.5358, -0.3574],\n",
       "         [ 1.6242, -1.1176]],\n",
       "\n",
       "        [[ 0.9376, -0.5317],\n",
       "         [-0.3598,  1.4796]],\n",
       "\n",
       "        [[-0.4084, -0.3343],\n",
       "         [-0.8405,  1.0805]],\n",
       "\n",
       "        [[ 2.8560,  1.2970],\n",
       "         [ 0.1296, -0.3994]],\n",
       "\n",
       "        [[ 0.0426, -0.7548],\n",
       "         [-0.8332,  0.5588]],\n",
       "\n",
       "        [[-0.6677,  1.3362],\n",
       "         [ 2.1013,  1.5137]],\n",
       "\n",
       "        [[ 0.9929,  0.4807],\n",
       "         [-0.5259,  1.0488]]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(10,2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "89137fb4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.9339, -0.3321],\n",
       "        [ 0.2666,  1.0373],\n",
       "        [ 0.7444,  0.8821],\n",
       "        [ 0.7015,  0.3231],\n",
       "        [ 0.3332, -1.2083],\n",
       "        [ 0.9383, -0.3344],\n",
       "        [ 0.3378,  0.1736],\n",
       "        [-0.1478,  0.9214],\n",
       "        [ 0.4617,  0.0070],\n",
       "        [-0.6755, -0.5433]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mean(torch.randn(10,3,2),axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "b4f3ba8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.0660, -0.1816,  0.5822])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mean(torch.randn(3,2),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b4d13f37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.0787])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mean(torch.randn(10,1),axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "473fc55f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[-1.9573, -1.0600,  0.3993,  0.2060],\n",
       "         [ 1.5810,  0.0837, -0.7011, -0.0529],\n",
       "         [ 0.7596, -1.5099,  1.4213, -1.0728]],\n",
       "\n",
       "        [[ 1.3687, -0.0109, -0.8908, -0.1336],\n",
       "         [ 0.0320,  1.0226, -0.4446, -0.4905],\n",
       "         [-1.0081,  1.0063,  0.8635,  0.0026]]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(2,3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "8ff76523",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.0525,  1.5126, -1.3933, -0.9546],\n",
       "        [ 0.5481, -0.2132, -0.0564, -0.6170]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(2,3,4)[:,-1,:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7c80a791",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.3367,  0.1288,  0.2345],\n",
       "        [ 0.2303, -1.1229, -0.1863]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.manual_seed(42)\n",
    "torch.randn(2,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0d01765",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 1.1103, -1.6898, -0.9890],\n",
       "        [ 0.9580,  1.3221,  0.8172]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07ee72a8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch_py38",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.7rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
